package org.xdq.juc的读写锁;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Test2 {

    static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    static void f1(){
        readWriteLock.writeLock().lock();
        try {
            System.out.println("f1-------------1");
            Thread.yield();
            System.out.println("f1-------------2");
            Thread.yield();
            System.out.println("f1-------------3");
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }

    static void f2(){
        readWriteLock.writeLock().lock();
        try {
            System.out.println("f2-1");
            Thread.yield();
            System.out.println("f2-2");
            Thread.yield();
            System.out.println("f2-3");
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }


    public static void main(String[] args) {
        Thread t1= new Thread(()->{
            for(int i=0;i<5000;i++){
               f1();
            }
        });
        Thread t2 = new Thread(()->{
            for(int i=0;i<5000;i++){
               f2();
            }
        });

        t1.start();
        t2.start();
    }
}
